package mapreduce.LogFormat;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

public class LogRecordWrite extends RecordWriter<Text, NullWritable> {
    FSDataOutputStream atguiguOut;
    FSDataOutputStream otherOut;

    public LogRecordWrite(TaskAttemptContext job) throws IOException {
        FileSystem fs = FileSystem.get(job.getConfiguration());
        atguiguOut = fs.create(new Path("D:\\mapreduceTest\\website\\atguigu.log"));
        otherOut = fs.create(new Path("D:\\mapreduceTest\\website\\other.log"));
    }


    @Override
    public void write(Text text, NullWritable nullWritable) throws IOException, InterruptedException {
        String s = text.toString();
        if (s.contains("atguigu")){
            atguiguOut.writeBytes(s+"\n");
        }else {
            otherOut.writeBytes(s+"\n");
        }
    }

    @Override
    public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        IOUtils.closeStream(atguiguOut);
        IOUtils.closeStream(otherOut);
    }
}
